CmdUtils.CreateCommand({
  names: ["langid", "which-language"],
  icon: "http://langid.net/favicon.ico",
  description: "LangId is a free web service that allows you to identify in which language was written any text you need to analyze.<br/><br/>Just select/type some text and wait to see the result",  
  help: "This command has two names - langid and which-language.<br/><br/>" + 
        "langid {selected text OR enter the text}<br/>OR<br/>which-language {selected text or enter the text}<br/><br/>" + 
        "<u>Note</u>: <ul><li>The text can be a maximum of 199 character long.</li></ul>" 
        ,
  author: {name: "MK", email: "mskadu+ubiquity@gmail.com"},
  license: "GPL",
  homepage: "http://langid.net/",
  arguments: [{role: 'object', nountype: noun_arb_text}],
  preview: function preview(pblock, args) {
    pblock.innerHTML = "Your input is <b>" + args.object.html + "</b>.";

    if( args.object.text)
    {
      var escapedText = escape( args.object.text ); //sanitize input text

      CmdUtils.previewGet(pblock, 
                         "http://api.langid.net/identify.json", 
                         {string: escapedText },
                         function( jsonIdentity ) 
                         {
                          if( jsonIdentity['error'] != null )
                          {
                            //call was ok - web-service returned error
                            pblock.innerHTML = "Sorry: " + jsonIdentity['error']['long'];
                          }
                          else if( jsonIdentity['response'] != null )
                          {
                           var sLanguageName = jsonIdentity['response']['full-name'];
                           var sImageURL = jsonIdentity['response']['img'];

                           pblock.innerHTML += "<p>Language: " + sLanguageName + "<br/>"
                                            + "<img src='" + sImageURL + "' />";
                          }
                         
                         },
                         "json"
      );
    }                   
  },
  execute: function execute(args) {
    if( args.object.text )
    {
      var url = "http://langid.net/?text=" + escape( args.object.text );
      Utils.openUrlInBrowser( url );
    }
    else
      displayMessage( _("No text to identify"), this );
  }
});
